<template>
  <div class="contenain">
    <div class="big-box">
      <!-- 图标 -->
      <div class="log">

        <img src="../../assets/logo.png" alt="">
      </div>
      <!-- form表单 -->
      <el-form :model="LogruleForm" label-width="75px" class="log-ruleForm" ref="loginRef" :rules="Loginrules">
        <el-form-item label="登录名" prop="username">
          <el-input prefix-icon="el-icon-user-solid" v-model="LogruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input prefix-icon="el-icon-unlock" v-model="LogruleForm.password" type="password"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="loginBtn">登录</el-button>
          <el-button type="primary" @click="resetLogin">重置</el-button>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user.js'
export default {
  data () {
    return {
      Loginrules: {
        username: [{ required: true, message: '请输账号', trigger: 'blur' }, { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }, { min: 6, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur' }]
      },
      LogruleForm: {
        username: 'admin',
        password: '123456'
      }
    }
  },
  methods: {
    // 重置按钮
    resetLogin () {
      this.$refs.loginRef.resetFields()
      console.log(this)
    },
    // 登录按钮
    loginBtn () {
      this.$refs.loginRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await login(
          this.LogruleForm
        )
        console.log(res)
        if (res.meta.status !== 200) {
          this.$message.error('登录失败')
        }
        this.$message.success('登录成功')
        console.log(res)
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.contenain {
  position: relative;
  height: 100%;
  background-color: #2b4b6b;
}
.big-box {
  position: absolute;
  background-color: #fff;
  width: 450px;
  height: 300px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.log {
  width: 150px;
  height: 150px;
  background-color: #fff;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  padding: 10px;
  transform: translate(-50%, -50%);
}
img {
  display: block;
  width: 100%;
  height: 100%;
  background-color: #ccc;
  border-radius: 50%;
}
// 表单
.log-ruleForm {
  position: absolute;
  bottom: 0;
  padding: 20px;
  width: 100%;
  box-sizing: border-box;
}
.btns {
  text-align: right;
}
</style>
